﻿using System.Windows;
using System.Windows.Input;

namespace Microsoft.Windows.UserControls.Core.Providers
{
    public class WizardCommandProvider : DependencyObject
    {
        public readonly static DependencyProperty WizardProperty = DependencyProperty.Register("Wizard", typeof(Wizard), typeof(WizardCommandProvider), new PropertyMetadata());

        public Wizard Wizard
        {
            get
            {
                return (Wizard)base.GetValue(WizardCommandProvider.WizardProperty);
            }
            set
            {
                base.SetValue(WizardCommandProvider.WizardProperty, value);
            }
        }

        public WizardCommandProvider(Wizard sender)
        {
            Wizard = sender;
        }

        public void RegisterCommandBindings()
        {
            Wizard.RegisterCommands();
        }

        protected internal virtual void Help()
        {
            if (Wizard == null)
                return;
            
            Wizard.OnHelpCommandExecute(new WizardButtonCommandEventArgs(Wizard.CurrentPageIndex, WizardButton.ButtonCommandType.Help));
        }

        protected internal virtual bool CanHelpExecute()
        {
            if (Wizard == null)
                return false;
            return Wizard.CanHelpExecuteCommand;
        }

        protected internal virtual void Previous()
        {
            if (Wizard == null)
                return;
            Wizard.OnPreviousCommandExecute(new WizardButtonCommandEventArgs(Wizard.CurrentPageIndex, WizardButton.ButtonCommandType.Previous));
        }

        protected internal virtual bool CanPreviousExecute()
        {
            if (Wizard == null)
                return false;
            return Wizard.CanPreviousExecuteCommand;
        }
        
        protected internal virtual void Next()
        {
            if (Wizard == null)
                return;
            Wizard.OnNextCommandExecute(new WizardButtonCommandEventArgs(Wizard.CurrentPageIndex, WizardButton.ButtonCommandType.Next));
        }
       
        protected internal virtual bool CanNextExecute()
        {
            if (Wizard == null)
                return false;
            return Wizard.CanNextExecuteCommand;
        }
        
        protected internal virtual void Cancel()
        {
            if (Wizard == null)
                return;
            Wizard.OnCancelCommandExecute(new WizardButtonCommandEventArgs(Wizard.CurrentPageIndex, WizardButton.ButtonCommandType.Cancel));
        }
        
        protected internal virtual bool CanCancelExecute()
        {
            if (Wizard == null)
                return false;
            return Wizard.CanCancelExecuteCommand;
        }
        
        protected internal virtual void Finish()
        {
            if (Wizard == null)
                return;
            Wizard.OnFinishCommandExecute(new WizardButtonCommandEventArgs(Wizard.CurrentPageIndex, WizardButton.ButtonCommandType.Finish));
        }
       
        protected internal virtual bool CanFinishExecute()
        {
            if (Wizard == null)
                return false;
            return Wizard.CanFinishExecuteCommand;
        }
      
        internal static void Register(ICommand command, ExecutedRoutedEventHandler executedHandler, CanExecuteRoutedEventHandler canExecuteHandler)
        {
            CommandManager.RegisterClassCommandBinding(typeof(Wizard), new CommandBinding(command, executedHandler, canExecuteHandler));
        }
    }
}